<template>
	<div style="display: flex;">
		<h3 style="width: 30%;"></h3>
		<h3 style="width: 40%;">管理员 - 【 教师{{actmsg[actidx]}} 】</h3>
		<h3 style="width: 30%;">
			<button @click="go2Add()">教师添加</button>
		</h3>
	</div>
	<hr >
	<div style="display: flex;">
		<div style="width: 60%;">
			<table style="margin-left: 5%;line-height: 36px;">
				<tr>
					<td>教师ID：</td>
					<td>
						<input type="text" v-model="tea.uid" readonly/>
					</td>
				</tr>
				<tr>
					<td>教师姓名：</td>
					<td>
						<input type="text" v-model="tea.uname"/>
					</td>
				</tr>
				<tr v-if="actidx == 0">
					<td>教师密码：</td>
					<td>
						<input type="text" v-model="tea.pwd"/>
					</td>
				</tr>
				<tr>
					<td>教师手机号：</td>
					<td>
						<input type="text" v-model="tea.phone"/>
					</td>
				</tr>
				<tr>
					<td>学生头像：</td>
					<td>
						<input type="file" ref="img"/>
					</td>
				</tr>
				<tr v-if="actidx == 0">
					<td></td>
					<td><button @click="doAdd()">教师添加</button></td>
				</tr>
				<tr v-if="actidx == 1">
					<td></td>
					<td><button @click="doUpdate()">教师修改</button></td>
				</tr>
			</table>
		</div>
		<hr >
		<div style="width: 40%;">
			<img v-if="actidx == 1" style="width: 25vw;height: 25vw"
			v-bind:src="`${$ip}/api/public/showimg/${tea.pic}`">
		</div>
	</div>
</template>

<script setup>
	import axios from 'axios';
</script>
<script>
	export default{
		 data(){
			return{
				tea :{uid :0,uname :'',phone :'',pwd :'',pic :''},
				//定义一个数组来显示不同的提示信息
				actmsg: ['添加','修改'],
				// actmsg[] 的下标， 当前的状态的下标 2, 默认显示状态
				actidx : 0,
			} 
		 },
		 methods:{
			 go2Add(){
			 	console.log("教师去添加");
			 	this.actidx = 0;
			 	this.tea = {uid :0,uname :'',phone :'',pwd :'',pic :''};
			 	console.log(this.tea);
			 },
			 doAdd(){
			 	console.log("教师添加");
			 	console.log(this.tea);
			 	
			 	let fd = new FormData();
			 	fd.append("uname",this.tea.uname);
			 	fd.append("phone",this.tea.phone);
			 	fd.append("pwd",this.tea.pwd);
			 	
			 	let filex = this.$refs.img.files[0];
			 	fd.append("mypic",filex);
			 	//'multipart/form-data'
			 	this.$postf("/api/adm/tea/add",fd
			 	).then((resp)=>{
			 		console.log(resp);
			 		this.$router.push("/adm/teaShow");
					this.$store.state.erropen = true;
					this.$store.state.errmsg = "添加成功";
			 	})
			 },
			 doUpdate(){
			 	console.log("教师修改");
			 	console.log(this.tea);
			 	
			 	let fd = new FormData();
			 	fd.append("uid",this.tea.uid);
			 	fd.append("uname",this.tea.uname);
			 	fd.append("phone",this.tea.phone);
			 	fd.append("pwd",this.tea.pwd);
			 	
			 	let filex = this.$refs.img.files[0];
			 	fd.append("mypic",filex);
			 	
			 	this.$postf("/api/adm/tea/update",fd
			 	).then((resp)=>{
			 		console.log("教师修改成功");console.log(resp);
			 		this.$router.push("/adm/teaShow");
					this.$store.state.erropen = true;
					this.$store.state.errmsg = "修改成功";
			 	})
			 },
		 },
		 mounted() {
		 	console.log("初始化加载数据....");
		 	this.tea.uid = this.$route.query.id;
			//去修改
			if(this.tea.uid > 0){
				console.log("教师修改");
				this.actidx = 1;
				
				this.$get("/api/adm/tea/findById/"+this.tea.uid
				).then((resp)=>{
					console.log(resp.data.data);
					this.tea = resp.data.data;
				})
			}
			
		 },
	}
</script>
<style>
</style>
